package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.cartoon.CartoonBeanListView;
import com.taptech.doufu.bean.cartoon.CartoonListBean;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class CartoonBigImageView extends LinearLayout{
    private CartoonBeanListView imageModel;
    public CartoonBigImageView(Context context) {
        super(context);
    }

    public CartoonBigImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CartoonBigImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private Context context;
    private ImageView ivPicBg;

    public CartoonBigImageView(final Context context, CartoonListBean model){
        this(context);
        if (model != null && model.getList() != null && model.getList().size() > 0 &&
                model.getList().get(0) != null) {
            View view = LayoutInflater.from(context).inflate(R.layout.view_booklist_item, null);
            this.context = context;
            ivPicBg = (ImageView) view.findViewById(R.id.ivPicBg);
            ivPicBg.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    jump();
                }
            });
            changeDayModelBg();
            setImageHeight();
            imageModel = model.getList().get(0);
            updateData();

            int width = ScreenUtil.getScreenWidth(context);
            int height = (int) (width/3.0f);
            LayoutParams params = new LayoutParams(width, height);
            params.gravity = Gravity.CENTER;
            addView(view,params);
        }
    }

    private void setImageHeight() {
        int width =ScreenUtil.getScreenWidth(context)- ScreenUtil.dip2px(context,24);
        int height = (int) (width/3.0f);
        LayoutParams params = new LayoutParams(width, height);
        ivPicBg.setLayoutParams(params);
    }

    private void jump() {
        if (imageModel == null) {
            return;
        }
        CartoonServices.clickContentCard(context,imageModel.getId(),Integer.parseInt(imageModel.getObject_type()),imageModel.getTarget_url(),imageModel.getTitle());
    }

    private void updateData() {
        if (imageModel != null) {
            GlideUtil.displayImage(ivPicBg,imageModel.getImage(),R.drawable.img_big_pic_default_loading);
        }
    }


    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        ivPicBg.setAlpha(1.0f);
    }

    private void darkBg() {
        ivPicBg.setAlpha(Constant.imagViewAlpha);
    }
}
